mui.init();
mui.plusReady(function() {
	var settings = app.getSettings();
	var state = app.getState();
	var uid = state.user_id;
	var keyword = plus.webview.currentWebview().keyword;
	if(keyword) {
		if(keyword < 10) {
			mui.toast('请输入正确的居民证');
		} else {
			searchfriend(keyword);
		}

	}
	document.getElementById('searchInput').addEventListener('keyup', searchName);
	document.getElementById('searchBtn').addEventListener('tap', searchFunc);

	function searchName(e) {
		//console.log(e.keyCode);
		if(e.keyCode == 13) {
			searchFunc();
		}
	}

	function searchFunc() {
		var keyword = document.getElementById('searchInput').value;
		if(keyword) {
			if(keyword < 10) {
				mui.toast('请输入正确的 CC 居民证');
			} else {
				searchfriend(keyword);
			}

		}
	}

	renderlist();
	/**
	 * 渲染列表
	 */
	function renderlist() {
		var getUrl = apiurl + "user/getfriends/?user_id=" + uid;
		mui.getJSON(getUrl, function(res) {
			document.querySelector("#list_area").innerHTML = template('list-template', {
				"flist": res.list
			});
			var passbtn = document.querySelector(".graybutton");
			if(passbtn) {
				passbtn.addEventListener('tap', passFriend);
			}

		});
	}
	mui("#list_area").on("tap", ".canclick", function() {
		var fid = this.getAttribute("fid");
		var head = this.getAttribute("head");
		var fname = this.getAttribute("title");
		var phone = this.getAttribute("phone");
		var targets = '/pages/user/other_detail.html';
		mui.openWindow({
			url: targets,
			id: targets,
			extras: {
				fid: fid,
				head: head,
				fname: fname,
				phone: phone,
				back_id: 'newfriend'
			}
		});

	});

	function passFriend(e) {
		var that = this;
		var postdata = {
			fid: that.getAttribute('fid'),
			user_id: uid
		};
		console.log(JSON.stringify(postdata));
		console.log(apiurl + 'user/passfriend');
		mui.post(apiurl + 'user/passfriend', postdata, function(res) {
			console.log(JSON.stringify(res));
			if(res.status > 0) {
				//mui.alert("请等待好友通过！");
				that.setAttribute('class', 'add');
				that.innerHTML = '已添加';
				var mainid = localStorage.getItem('mainpage');
				var mainWin = plus.webview.getWebviewById(mainid);
				mui.fire(mainWin, 'passFriends', res.data);
				var chatWin = plus.webview.getWebviewById('chat.html');
				mui.fire(chatWin, 'passFriends', null);
				
				renderlist();
				//var findwin = plus.webview.getWebviewById("contact.html");
				//mui.fire(findwin, 'passFriends', postdata);
				//var chatWin = plus.webview.getWebviewById('chat.html');
				//mui.fire(chatWin, 'refreshList', null);
			} else {
				mui.alert(res.msg);
			}
		});
	}

	function searchfriend(keyword) {

		var postdata = {
			keyword: keyword,
			user_id: uid
		};

		console.log(apiurl + 'user/searchfriend'+'?keyword='+keyword+ '&user_id='+uid);
		mui.post(apiurl + 'user/searchfriend', postdata, function(res) {
			console.log(JSON.stringify(res));
			if(res.info) {
				document.querySelector("#add_area").innerHTML = template('add-template', {
					"item": res.info
				});
			}
			if(res.status > 0) {

				document.querySelector("#addnewFriend").addEventListener('tap', addnewFriend);
			} else {
				mui.alert(res.msg);
			}
		});
	}

	function addnewFriend(e) {
		var that = this;
		var fid = that.getAttribute('fid');

		var postdata = {
			fid: fid,
			user_id: uid
		};
		//console.log(uid);
		mui.post(apiurl + 'user/addfriends', postdata, function(res) {
			console.log(JSON.stringify(res));
			if(res.status > 0) {
				mui.alert("请等待好友通过！");
				that.setAttribute('class', 'add');
				that.innerHTML = '申请中';
				var mainid = localStorage.getItem('mainpage');
				var mainWin = plus.webview.getWebviewById(mainid);
				postdata.type = 'addfriends';
				mui.fire(mainWin, 'addFriends', postdata);
				//var findwin = plus.webview.getWebviewById("contact.html");
				//mui.fire(findwin, 'hasFriends', postdata);
			} else {
				mui.alert(res.msg);
			}
		});
		//console.log(fid);
	}

	window.addEventListener('refreshList', function(event) {
		renderlist();
	});
});